package o;

import com.huawei.hwid.core.constants.HwAccountConstants;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Flushable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

public final class fvh implements Closeable, Flushable {
    static final Pattern ˋ = Pattern.compile("[a-z0-9_-]{1,120}");
    static final /* synthetic */ boolean ͺ = (!fvh.class.desiredAssertionStatus());
    final LinkedHashMap<String, c> ʻ = new LinkedHashMap(0, 0.75f, true);
    private final int ʻॱ;
    int ʼ;
    boolean ʽ;
    private final Runnable ʾ = new Runnable(this) {
        final /* synthetic */ fvh ˏ;

        {
            this.ˏ = r1;
        }

        public void run() {
            int i = 1;
            synchronized (this.ˏ) {
                if (this.ˏ.ᐝ) {
                    i = 0;
                }
                if ((i | this.ˏ.ʽ) != 0) {
                    return;
                }
                try {
                    this.ˏ.ˊ();
                } catch (IOException e) {
                    this.ˏ.ॱˊ = true;
                }
                try {
                    if (this.ˏ.ˏ()) {
                        this.ˏ.ˎ();
                        this.ˏ.ʼ = 0;
                    }
                } catch (IOException e2) {
                    this.ˏ.ˊॱ = true;
                    this.ˏ.ॱ = fxr.ˋ(fxr.ˎ());
                }
            }
        }
    };
    private final Executor ˈ;
    final File ˊ;
    boolean ˊॱ;
    private final File ˋॱ;
    final int ˎ;
    final fwm ˏ;
    private final File ˏॱ;
    fxj ॱ;
    boolean ॱˊ;
    private long ॱˋ = 0;
    private long ॱˎ;
    boolean ॱॱ;
    private final File ॱᐝ;
    boolean ᐝ;
    private long ᐝॱ = 0;

    public final class a implements Closeable {
        private final long[] ˊ;
        private final fxw[] ˋ;
        private final String ˎ;
        final /* synthetic */ fvh ˏ;
        private final long ॱ;

        a(fvh o_fvh, String str, long j, fxw[] o_fxwArr, long[] jArr) {
            this.ˏ = o_fvh;
            this.ˎ = str;
            this.ॱ = j;
            this.ˋ = o_fxwArr;
            this.ˊ = jArr;
        }

        @Nullable
        public d ˋ() throws IOException {
            return this.ˏ.ॱ(this.ˎ, this.ॱ);
        }

        public fxw ˋ(int i) {
            return this.ˋ[i];
        }

        public void close() {
            for (Closeable ॱ : this.ˋ) {
                fvg.ॱ(ॱ);
            }
        }
    }

    final class c {
        final /* synthetic */ fvh ʻ;
        d ʽ;
        final long[] ˊ;
        boolean ˋ;
        final String ˎ;
        final File[] ˏ;
        final File[] ॱ;
        long ॱॱ;

        c(fvh o_fvh, String str) {
            this.ʻ = o_fvh;
            this.ˎ = str;
            this.ˊ = new long[o_fvh.ˎ];
            this.ˏ = new File[o_fvh.ˎ];
            this.ॱ = new File[o_fvh.ˎ];
            StringBuilder append = new StringBuilder(str).append('.');
            int length = append.length();
            for (int i = 0; i < o_fvh.ˎ; i++) {
                append.append(i);
                this.ˏ[i] = new File(o_fvh.ˊ, append.toString());
                append.append(".tmp");
                this.ॱ[i] = new File(o_fvh.ˊ, append.toString());
                append.setLength(length);
            }
        }

        void ˋ(String[] strArr) throws IOException {
            if (strArr.length != this.ʻ.ˎ) {
                throw ॱ(strArr);
            }
            int i = 0;
            while (i < strArr.length) {
                try {
                    this.ˊ[i] = Long.parseLong(strArr[i]);
                    i++;
                } catch (NumberFormatException e) {
                    throw ॱ(strArr);
                }
            }
        }

        void ॱ(fxj o_fxj) throws IOException {
            for (long ˋॱ : this.ˊ) {
                o_fxj.ॱॱ(32).ˋॱ(ˋॱ);
            }
        }

        private IOException ॱ(String[] strArr) throws IOException {
            throw new IOException("unexpected journal line: " + Arrays.toString(strArr));
        }

        a ˏ() {
            int i = 0;
            if (Thread.holdsLock(this.ʻ)) {
                fxw[] o_fxwArr = new fxw[this.ʻ.ˎ];
                long[] jArr = (long[]) this.ˊ.clone();
                int i2 = 0;
                while (i2 < this.ʻ.ˎ) {
                    try {
                        o_fxwArr[i2] = this.ʻ.ˏ.ˊ(this.ˏ[i2]);
                        i2++;
                    } catch (FileNotFoundException e) {
                        while (i < this.ʻ.ˎ && o_fxwArr[i] != null) {
                            fvg.ॱ(o_fxwArr[i]);
                            i++;
                        }
                        try {
                            this.ʻ.ˊ(this);
                        } catch (IOException e2) {
                        }
                        return null;
                    }
                }
                return new a(this.ʻ, this.ˎ, this.ॱॱ, o_fxwArr, jArr);
            }
            throw new AssertionError();
        }
    }

    public final class d {
        final boolean[] ˊ;
        private boolean ˎ;
        final /* synthetic */ fvh ˏ;
        final c ॱ;

        d(fvh o_fvh, c cVar) {
            this.ˏ = o_fvh;
            this.ॱ = cVar;
            this.ˊ = cVar.ˋ ? null : new boolean[o_fvh.ˎ];
        }

        void ˎ() {
            if (this.ॱ.ʽ == this) {
                for (int i = 0; i < this.ˏ.ˎ; i++) {
                    try {
                        this.ˏ.ˏ.ॱ(this.ॱ.ॱ[i]);
                    } catch (IOException e) {
                    }
                }
                this.ॱ.ʽ = null;
            }
        }

        public fxx ˏ(int i) {
            fxx ˎ;
            synchronized (this.ˏ) {
                if (this.ˎ) {
                    throw new IllegalStateException();
                } else if (this.ॱ.ʽ != this) {
                    ˎ = fxr.ˎ();
                } else {
                    if (!this.ॱ.ˋ) {
                        this.ˊ[i] = true;
                    }
                    try {
                        ˎ = new fvk(this, this.ˏ.ˏ.ˋ(this.ॱ.ॱ[i])) {
                            final /* synthetic */ d ˊ;

                            protected void ˊ(IOException iOException) {
                                synchronized (this.ˊ.ˏ) {
                                    this.ˊ.ˎ();
                                }
                            }
                        };
                    } catch (FileNotFoundException e) {
                        ˎ = fxr.ˎ();
                    }
                }
            }
            return ˎ;
        }

        public void ॱ() throws IOException {
            synchronized (this.ˏ) {
                if (this.ˎ) {
                    throw new IllegalStateException();
                }
                if (this.ॱ.ʽ == this) {
                    this.ˏ.ॱ(this, true);
                }
                this.ˎ = true;
            }
        }

        public void ˊ() throws IOException {
            synchronized (this.ˏ) {
                if (this.ˎ) {
                    throw new IllegalStateException();
                }
                if (this.ॱ.ʽ == this) {
                    this.ˏ.ॱ(this, false);
                }
                this.ˎ = true;
            }
        }
    }

    fvh(fwm o_fwm, File file, int i, int i2, long j, Executor executor) {
        this.ˏ = o_fwm;
        this.ˊ = file;
        this.ʻॱ = i;
        this.ˋॱ = new File(file, "journal");
        this.ˏॱ = new File(file, "journal.tmp");
        this.ॱᐝ = new File(file, "journal.bkp");
        this.ˎ = i2;
        this.ॱˎ = j;
        this.ˈ = executor;
    }

    public synchronized void ॱ() throws IOException {
        if (!ͺ && !Thread.holdsLock(this)) {
            throw new AssertionError();
        } else if (!this.ᐝ) {
            if (this.ˏ.ˎ(this.ॱᐝ)) {
                if (this.ˏ.ˎ(this.ˋॱ)) {
                    this.ˏ.ॱ(this.ॱᐝ);
                } else {
                    this.ˏ.ˊ(this.ॱᐝ, this.ˋॱ);
                }
            }
            if (this.ˏ.ˎ(this.ˋॱ)) {
                try {
                    ʼ();
                    ॱॱ();
                    this.ᐝ = true;
                } catch (Throwable e) {
                    fwy.ˋ().ˏ(5, "DiskLruCache " + this.ˊ + " is corrupt: " + e.getMessage() + ", removing", e);
                    ᐝ();
                    this.ʽ = false;
                } catch (Throwable th) {
                    this.ʽ = false;
                }
            }
            ˎ();
            this.ᐝ = true;
        }
    }

    public static fvh ˏ(fwm o_fwm, File file, int i, int i2, long j) {
        if (j <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        } else if (i2 <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        } else {
            return new fvh(o_fwm, file, i, i2, j, new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), fvg.ˊ("OkHttp DiskLruCache", true)));
        }
    }

    private void ʼ() throws IOException {
        Closeable ˎ = fxr.ˎ(this.ˏ.ˊ(this.ˋॱ));
        int i;
        try {
            String ॱˋ = ˎ.ॱˋ();
            String ॱˋ2 = ˎ.ॱˋ();
            String ॱˋ3 = ˎ.ॱˋ();
            String ॱˋ4 = ˎ.ॱˋ();
            String ॱˋ5 = ˎ.ॱˋ();
            if ("libcore.io.DiskLruCache".equals(ॱˋ) && "1".equals(ॱˋ2) && Integer.toString(this.ʻॱ).equals(ॱˋ3) && Integer.toString(this.ˎ).equals(ॱˋ4) && "".equals(ॱˋ5)) {
                i = 0;
                while (true) {
                    ˊ(ˎ.ॱˋ());
                    i++;
                }
            } else {
                throw new IOException("unexpected journal header: [" + ॱˋ + ", " + ॱˋ2 + ", " + ॱˋ4 + ", " + ॱˋ5 + "]");
            }
        } catch (EOFException e) {
            this.ʼ = i - this.ʻ.size();
            if (ˎ.ᐝ()) {
                this.ॱ = ʻ();
            } else {
                ˎ();
            }
            fvg.ॱ(ˎ);
        } catch (Throwable th) {
            fvg.ॱ(ˎ);
        }
    }

    private fxj ʻ() throws FileNotFoundException {
        return fxr.ˋ(new fvk(this, this.ˏ.ˏ(this.ˋॱ)) {
            static final /* synthetic */ boolean ˏ = (!fvh.class.desiredAssertionStatus());
            final /* synthetic */ fvh ॱ;

            protected void ˊ(IOException iOException) {
                if (ˏ || Thread.holdsLock(this.ॱ)) {
                    this.ॱ.ॱॱ = true;
                    return;
                }
                throw new AssertionError();
            }
        });
    }

    private void ˊ(String str) throws IOException {
        int indexOf = str.indexOf(32);
        if (indexOf == -1) {
            throw new IOException("unexpected journal line: " + str);
        }
        String str2;
        int i = indexOf + 1;
        int indexOf2 = str.indexOf(32, i);
        if (indexOf2 == -1) {
            String substring = str.substring(i);
            if (indexOf == "REMOVE".length() && str.startsWith("REMOVE")) {
                this.ʻ.remove(substring);
                return;
            }
            str2 = substring;
        } else {
            str2 = str.substring(i, indexOf2);
        }
        c cVar = (c) this.ʻ.get(str2);
        if (cVar == null) {
            cVar = new c(this, str2);
            this.ʻ.put(str2, cVar);
        }
        if (indexOf2 != -1 && indexOf == "CLEAN".length() && str.startsWith("CLEAN")) {
            String[] split = str.substring(indexOf2 + 1).split(HwAccountConstants.BLANK);
            cVar.ˋ = true;
            cVar.ʽ = null;
            cVar.ˋ(split);
        } else if (indexOf2 == -1 && indexOf == "DIRTY".length() && str.startsWith("DIRTY")) {
            cVar.ʽ = new d(this, cVar);
        } else if (indexOf2 != -1 || indexOf != "READ".length() || !str.startsWith("READ")) {
            throw new IOException("unexpected journal line: " + str);
        }
    }

    private void ॱॱ() throws IOException {
        this.ˏ.ॱ(this.ˏॱ);
        Iterator it = this.ʻ.values().iterator();
        while (it.hasNext()) {
            c cVar = (c) it.next();
            int i;
            if (cVar.ʽ == null) {
                for (i = 0; i < this.ˎ; i++) {
                    this.ᐝॱ += cVar.ˊ[i];
                }
            } else {
                cVar.ʽ = null;
                for (i = 0; i < this.ˎ; i++) {
                    this.ˏ.ॱ(cVar.ˏ[i]);
                    this.ˏ.ॱ(cVar.ॱ[i]);
                }
                it.remove();
            }
        }
    }

    synchronized void ˎ() throws IOException {
        if (this.ॱ != null) {
            this.ॱ.close();
        }
        fxj ˋ = fxr.ˋ(this.ˏ.ˋ(this.ˏॱ));
        try {
            ˋ.ˋ("libcore.io.DiskLruCache").ॱॱ(10);
            ˋ.ˋ("1").ॱॱ(10);
            ˋ.ˋॱ((long) this.ʻॱ).ॱॱ(10);
            ˋ.ˋॱ((long) this.ˎ).ॱॱ(10);
            ˋ.ॱॱ(10);
            for (c cVar : this.ʻ.values()) {
                if (cVar.ʽ != null) {
                    ˋ.ˋ("DIRTY").ॱॱ(32);
                    ˋ.ˋ(cVar.ˎ);
                    ˋ.ॱॱ(10);
                } else {
                    ˋ.ˋ("CLEAN").ॱॱ(32);
                    ˋ.ˋ(cVar.ˎ);
                    cVar.ॱ(ˋ);
                    ˋ.ॱॱ(10);
                }
            }
            ˋ.close();
            if (this.ˏ.ˎ(this.ˋॱ)) {
                this.ˏ.ˊ(this.ˋॱ, this.ॱᐝ);
            }
            this.ˏ.ˊ(this.ˏॱ, this.ˋॱ);
            this.ˏ.ॱ(this.ॱᐝ);
            this.ॱ = ʻ();
            this.ॱॱ = false;
            this.ˊॱ = false;
        } catch (Throwable th) {
            ˋ.close();
        }
    }

    public synchronized a ˏ(String str) throws IOException {
        a aVar;
        ॱ();
        ʽ();
        ˎ(str);
        c cVar = (c) this.ʻ.get(str);
        if (cVar == null || !cVar.ˋ) {
            aVar = null;
        } else {
            aVar = cVar.ˏ();
            if (aVar == null) {
                aVar = null;
            } else {
                this.ʼ++;
                this.ॱ.ˋ("READ").ॱॱ(32).ˋ(str).ॱॱ(10);
                if (ˏ()) {
                    this.ˈ.execute(this.ʾ);
                }
            }
        }
        return aVar;
    }

    @Nullable
    public d ˋ(String str) throws IOException {
        return ॱ(str, -1);
    }

    synchronized d ॱ(String str, long j) throws IOException {
        d dVar;
        ॱ();
        ʽ();
        ˎ(str);
        c cVar = (c) this.ʻ.get(str);
        if (j == -1 || (cVar != null && cVar.ॱॱ == j)) {
            if (cVar != null) {
                if (cVar.ʽ != null) {
                    dVar = null;
                }
            }
            if (this.ॱˊ || this.ˊॱ) {
                this.ˈ.execute(this.ʾ);
                dVar = null;
            } else {
                this.ॱ.ˋ("DIRTY").ॱॱ(32).ˋ(str).ॱॱ(10);
                this.ॱ.flush();
                if (this.ॱॱ) {
                    dVar = null;
                } else {
                    c cVar2;
                    if (cVar == null) {
                        cVar = new c(this, str);
                        this.ʻ.put(str, cVar);
                        cVar2 = cVar;
                    } else {
                        cVar2 = cVar;
                    }
                    dVar = new d(this, cVar2);
                    cVar2.ʽ = dVar;
                }
            }
        } else {
            dVar = null;
        }
        return dVar;
    }

    synchronized void ॱ(d dVar, boolean z) throws IOException {
        int i = 0;
        synchronized (this) {
            c cVar = dVar.ॱ;
            if (cVar.ʽ != dVar) {
                throw new IllegalStateException();
            }
            if (z) {
                if (!cVar.ˋ) {
                    int i2 = 0;
                    while (i2 < this.ˎ) {
                        if (!dVar.ˊ[i2]) {
                            dVar.ˊ();
                            throw new IllegalStateException("Newly created entry didn't create value for index " + i2);
                        } else if (!this.ˏ.ˎ(cVar.ॱ[i2])) {
                            dVar.ˊ();
                            break;
                        } else {
                            i2++;
                        }
                    }
                }
            }
            while (i < this.ˎ) {
                File file = cVar.ॱ[i];
                if (!z) {
                    this.ˏ.ॱ(file);
                } else if (this.ˏ.ˎ(file)) {
                    File file2 = cVar.ˏ[i];
                    this.ˏ.ˊ(file, file2);
                    long j = cVar.ˊ[i];
                    long ʻ = this.ˏ.ʻ(file2);
                    cVar.ˊ[i] = ʻ;
                    this.ᐝॱ = (this.ᐝॱ - j) + ʻ;
                }
                i++;
            }
            this.ʼ++;
            cVar.ʽ = null;
            if ((cVar.ˋ | z) != 0) {
                cVar.ˋ = true;
                this.ॱ.ˋ("CLEAN").ॱॱ(32);
                this.ॱ.ˋ(cVar.ˎ);
                cVar.ॱ(this.ॱ);
                this.ॱ.ॱॱ(10);
                if (z) {
                    long j2 = this.ॱˋ;
                    this.ॱˋ = 1 + j2;
                    cVar.ॱॱ = j2;
                }
            } else {
                this.ʻ.remove(cVar.ˎ);
                this.ॱ.ˋ("REMOVE").ॱॱ(32);
                this.ॱ.ˋ(cVar.ˎ);
                this.ॱ.ॱॱ(10);
            }
            this.ॱ.flush();
            if (this.ᐝॱ > this.ॱˎ || ˏ()) {
                this.ˈ.execute(this.ʾ);
            }
        }
    }

    boolean ˏ() {
        return this.ʼ >= 2000 && this.ʼ >= this.ʻ.size();
    }

    public synchronized boolean ॱ(String str) throws IOException {
        boolean z;
        ॱ();
        ʽ();
        ˎ(str);
        c cVar = (c) this.ʻ.get(str);
        if (cVar == null) {
            z = false;
        } else {
            z = ˊ(cVar);
            if (z && this.ᐝॱ <= this.ॱˎ) {
                this.ॱˊ = false;
            }
        }
        return z;
    }

    boolean ˊ(c cVar) throws IOException {
        if (cVar.ʽ != null) {
            cVar.ʽ.ˎ();
        }
        for (int i = 0; i < this.ˎ; i++) {
            this.ˏ.ॱ(cVar.ˏ[i]);
            this.ᐝॱ -= cVar.ˊ[i];
            cVar.ˊ[i] = 0;
        }
        this.ʼ++;
        this.ॱ.ˋ("REMOVE").ॱॱ(32).ˋ(cVar.ˎ).ॱॱ(10);
        this.ʻ.remove(cVar.ˎ);
        if (ˏ()) {
            this.ˈ.execute(this.ʾ);
        }
        return true;
    }

    public synchronized boolean ˋ() {
        return this.ʽ;
    }

    private synchronized void ʽ() {
        if (ˋ()) {
            throw new IllegalStateException("cache is closed");
        }
    }

    public synchronized void flush() throws IOException {
        if (this.ᐝ) {
            ʽ();
            ˊ();
            this.ॱ.flush();
        }
    }

    public synchronized void close() throws IOException {
        if (!this.ᐝ || this.ʽ) {
            this.ʽ = true;
        } else {
            for (c cVar : (c[]) this.ʻ.values().toArray(new c[this.ʻ.size()])) {
                if (cVar.ʽ != null) {
                    cVar.ʽ.ˊ();
                }
            }
            ˊ();
            this.ॱ.close();
            this.ॱ = null;
            this.ʽ = true;
        }
    }

    void ˊ() throws IOException {
        while (this.ᐝॱ > this.ॱˎ) {
            ˊ((c) this.ʻ.values().iterator().next());
        }
        this.ॱˊ = false;
    }

    public void ᐝ() throws IOException {
        close();
        this.ˏ.ʼ(this.ˊ);
    }

    private void ˎ(String str) {
        if (!ˋ.matcher(str).matches()) {
            throw new IllegalArgumentException("keys must match regex [a-z0-9_-]{1,120}: \"" + str + "\"");
        }
    }
}
